package com.ruoyi.draw.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.draw.domain.NovelStoryboard;
import com.ruoyi.draw.service.INovelStoryboardService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * NovelStoryboardController
 * 
 * @author Locke
 * @date 2025-10-25
 */
@RestController
@RequestMapping("/novel/storyboard")
public class NovelStoryboardController extends BaseController
{
    @Autowired
    private INovelStoryboardService novelStoryboardService;

    /**
     * 查询NovelStoryboard列表
     */
    @PreAuthorize("@ss.hasPermi('novel:storyboard:list')")
    @GetMapping("/list")
    public TableDataInfo list(NovelStoryboard novelStoryboard)
    {
        startPage();
        List<NovelStoryboard> list = novelStoryboardService.selectNovelStoryboardList(novelStoryboard);
        return getDataTable(list);
    }

    /**
     * 导出NovelStoryboard列表
     */
    @PreAuthorize("@ss.hasPermi('novel:storyboard:export')")
    @Log(title = "NovelStoryboard", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NovelStoryboard novelStoryboard)
    {
        List<NovelStoryboard> list = novelStoryboardService.selectNovelStoryboardList(novelStoryboard);
        ExcelUtil<NovelStoryboard> util = new ExcelUtil<NovelStoryboard>(NovelStoryboard.class);
        util.exportExcel(response, list, "NovelStoryboard数据");
    }

    /**
     * 获取NovelStoryboard详细信息
     */
    @PreAuthorize("@ss.hasPermi('novel:storyboard:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(novelStoryboardService.selectNovelStoryboardById(id));
    }

    /**
     * 新增NovelStoryboard
     */
    @PreAuthorize("@ss.hasPermi('novel:storyboard:add')")
    @Log(title = "NovelStoryboard", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NovelStoryboard novelStoryboard)
    {
        return toAjax(novelStoryboardService.insertNovelStoryboard(novelStoryboard));
    }

    /**
     * 修改NovelStoryboard
     */
    @PreAuthorize("@ss.hasPermi('novel:storyboard:edit')")
    @Log(title = "NovelStoryboard", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NovelStoryboard novelStoryboard)
    {
        return toAjax(novelStoryboardService.updateNovelStoryboard(novelStoryboard));
    }

    /**
     * 删除NovelStoryboard
     */
    @PreAuthorize("@ss.hasPermi('novel:storyboard:remove')")
    @Log(title = "NovelStoryboard", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(novelStoryboardService.deleteNovelStoryboardByIds(ids));
    }
    
    /**
     * 从docx文件中提取文字
     */
    @PreAuthorize("@ss.hasPermi('novel:storyboard:extract')")
    @Log(title = "NovelStoryboard", businessType = BusinessType.OTHER)
    @PostMapping("/extract-text")
    public AjaxResult extractDocxText(String url)
    {
        try {
            String extractedText = novelStoryboardService.extractTextFromDocx(url);
            return AjaxResult.success("提取成功", extractedText);
        } catch (Exception e) {
            return AjaxResult.error("提取失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据小说ID和章节ID查询分镜列表
     */
    @PreAuthorize("@ss.hasPermi('novel:storyboard:list')")
    @GetMapping("/list/{novelId}/{chapterId}")
    public AjaxResult listByNovelAndChapter(@PathVariable("novelId") Long novelId, 
                                            @PathVariable("chapterId") Long chapterId)
    {
        List<NovelStoryboard> list = novelStoryboardService.selectNovelStoryboardByNovelIdAndChapter(novelId, chapterId);
        return success(list);
    }
}